﻿/**
 * Column handles the columns of content in the editor
*/
dynamic class Column extends MovieClip {
	
	// Constants
	var padding:Number = 10;
	
	// Variables
	var column_number:Number;
	var element_array:Array;
	var column_width_percent:Number;
	var column_width_actual:Number;
	var widest_element:Number;
	var render_width:Number;
	var column_x:Number;

	/**
	 * Column instantiates the Column
	*/
	function Column() {
		
		// Initialise
		this.element_array = new Array();
		this.column_width_actual = 0;
		this.widest_element = 0;
		this.render_width = 0;
		this.column_x = 0;
		
		// If it is the first column, make 70% of width, otherwise 30%
		if (this.column_number == 1) {
			this.column_width_percent = 70;
		} else {
			this.column_width_percent = 30;
		}
	}

	/**
	 * resetLayout() resets the column layout
	*/
	function resetLayout():Void {
		
		// Reset array of elements
		this.element_array = new Array();
		
		// Reset widest element
		this.widest_element = 0;
	}

	/**
	 * addElement() adds an element to this columns array
	*/
	function addElement(element_mc:MovieClip) {
		
		// Add element to array
		this.element_array.push(element_mc);
		
		// Get the minimum width of this element
		var element_minimum_width:Number = element_mc.getMinimumWidth();
		
		// If this is a new widest element, store it
		if (element_minimum_width > this.widest_element) {
			this.widest_element = element_minimum_width;
		}
	}
	
	/**
	 * drawBackground() draws the background if it hasn't been drawn already
	*/
	function drawBackground(width:Number, height:Number):Void {
		
		// If background doesn't exist yet, add now
		if (!this.background_mc) {
		
			// Ensure it's gone
			this.background_mc.removeMovieClip();
		
			// Attach a background for the column
			this.attachMovie("column_background", "background_mc", this.getNextHighestDepth(), {
				_width: width,
				_height: height
			});
		}
	}

	/**
	 * drawLayout() draws the column layout, redrawing each element inside it
	*/
	function drawLayout() {

		// Reference to each element movieclip
		var element_mc:MovieClip;
		
		// Column Height is a tally of the height of the column with all elements
		var column_height:Number = 0;
		
		// Determine width of column
		this.determineRenderWidth();
		
		// For each element
		for (var i:Number = 0; i < this.element_array.length; i++) {
			
			// Get reference to MC
			element_mc = this.element_array[i];
			
			// Redraw it with the column width
			element_mc.redraw(this.render_width);
			
			// Set X value
			element_mc._x = this.column_x;
			
			// Set Y value
			element_mc._y = column_height + this.padding;
			
			// Tally column height
			column_height += element_mc._height + this.padding;
		}

		// Draw background
		drawBackground(render_width, column_height);
		
		// Set background to size
		this.background_mc._width = render_width;
		this.background_mc._height = column_height;
	}
	
	/**
	 * determineRenderWidth() determines the width of this column
	*/
	function determineRenderWidth() {
				
		// Get width as proportion of content area width
		column_width_actual = _root.getContentAreaWidth() * (this.column_width_percent / 100);

		// If the widest element is wider then this width, use width of widest element
		if (this.widest_element > column_width_actual) {
			this.render_width = this.widest_element;
		} else {
			this.render_width = column_width_actual;
		}
	}

	/**
	 * getRenderWidthAdjustment() gets the difference between the render width and the actual width
	 *
	 * @return the difference
	*/
	function getRenderWidthAdjustment():Number {
		return this.render_width - this.column_width_actual;
	}

	/**
	 * setRenderWidthAdjustment() adds an adjustment value to the render width
	 * Don't know why.
	 *
	 * @param adjustment is the value to add to the render width
	*/
	function setRenderWidthAdjustment(adjustment:Number):Void {

		// Add adjustment to render width
		this.render_width += adjustment;
		
		// If render width is smaller than the widest element, increase it to fit
		if (this.render_width < this.widest_element) {
			this.render_width = this.widest_element;
		}
	}

	/**
	 * setWidthPercentage() sets the width percentage
	 *
	 * @param new_column_width_percent is the new value
	*/
	function setWidthPercentage(new_column_width_percent:Number):Void {
		this.column_width_percent = new_column_width_percent;
	}

	/**
	 * getWidthPercentage() gets the column width percent
	 *
	 * @return the column_width_percent
	*/
	function getWidthPercentage():Number {
		return this.column_width_percent;
	}

	/**
	 * getWidestElement() gets the width of the widest element
	 *
	 * @return the widest_element
	*/
	function getWidestElement():Number {
		return this.widest_element;
	}

	/**
	 * setColumnX() sets the column x
	 *
	 * @param new_column_x is the new column x
	*/
	function setColumnX(new_column_x:Number):Void {
		this.column_x = new_column_x;
	}

	/**
	 * getColumnX() gets the column x
	 *
	 * @return the column_x
	*/
	function getColumnX():Number {
		return this.column_x;
	}

	/**
	 * getRenderWidth() gets the render width
	 *
	 * @return the render_width
	*/
	function getRenderWidth():Number {
		return this.render_width;
	}

	/**
	 * getSource() gets the source for this column
	 *
	 * @return the source to save for this column
	*/
	function getSource():String {
		return "</column><column class=\"column2\">";
	}
	
	/**
	 * isColumn returns true
	 *
	 * @return true
	*/
	function isColumn() {
		return true;
	}
}